package com.csu.ourpetstore1.controller;

import com.csu.ourpetstore1.domain.Account;
import com.csu.ourpetstore1.service.AccountService;
import com.csu.ourpetstore1.util.OfAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;


@Controller
@RequestMapping("/account")
@SessionAttributes(value = {"username","verification_code_id"})
public class AccountController {
    @Autowired
    AccountService AccountService;
    //跳往注册界面
    @GetMapping("/GoToRegister")
    public  String GotoRegister(Model model){
        model.addAttribute("verification_code_id",(int)(1+Math.random()*3));
        return "account/newAccountForm";
    }
    //跳往登录界面
    @GetMapping("/SignIn")
    public  String GotoSignOn(Model model){
        model.addAttribute("verification_code_id",(int)(1+Math.random()*3));
        return "account/signonForm";
    }
    //跳往登录界面
    @GetMapping("/Signoff")
    public  String SignOff(SessionStatus sessionStatus){
        sessionStatus.setComplete();
        return "catalog/main";
    }
    //登录验证
    @PostMapping ("/checksignon")
    public  String SignOn(String username,String password,String verification_code,@SessionAttribute("verification_code_id") String verification_code_id, Model model){
        if(Integer.parseInt(verification_code_id)==1){
            if(verification_code.equals("z3cm")){
                if(AccountService.getAccount(username, password)==null){
                    return "account/signonForm";
                }
                else{
                    model.addAttribute("username", username);
                    return "catalog/main";
                }
            }
            else {

            }
        }
        if(Integer.parseInt(verification_code_id)==2){
            if(verification_code.equals("v33c")){
                if(AccountService.getAccount(username, password)==null){
                    return "account/signonForm";
                }
                else{
                    model.addAttribute("username", username);
                    return "catalog/main";
                }
            }
            else {

            }
        }
        if(Integer.parseInt(verification_code_id)==3){
            if(verification_code.equals("xvv2")){
                if(AccountService.getAccount(username, password)==null){
                    return "account/signonForm";
                }
                else{
                    model.addAttribute("username", username);
                    return "catalog/main";
                }
            }
            else {

            }
        }
        if(Integer.parseInt(verification_code_id)==4) {
            if (verification_code.equals("zm2m")) {
                if (AccountService.getAccount(username, password) == null) {
                    return "account/signonForm";
                } else {
                    model.addAttribute("username", username);
                    return "catalog/main";
                }
            } else {

            }
        }
        return "commom/error";
    }
    //跳往编辑界面
    @GetMapping(value = "/GoToEdit")
    public  String GotoEdit(String username,Model model){
        Account account = AccountService.getAccount(username);
        model.addAttribute("OldAccount", account);
        return "account/editAccountForm";
    }

    @GetMapping("checkRegister")
    @ResponseBody
    public Object CheckRegister(String username){
        OfAccount ofAccount = new OfAccount();

        if(username.length()==0){
            ofAccount.setCode(0);
            ofAccount.setMsg("The username can't be empty.");
        }
        else if(AccountService.getAccount(username)!=null){
            ofAccount.setCode(1);
            ofAccount.setMsg("The username is already used.");
        }else {
            ofAccount.setCode(2);
            ofAccount.setMsg("The username is available.");
        }

        return ofAccount;
    }

    @PostMapping("/register")
    public String Register(String verification_code,String repeatedPassword,@ModelAttribute Account account, @SessionAttribute("verification_code_id") String verification_code_id,Model model){
        if(Integer.parseInt(verification_code_id)==1&&verification_code.equals("z3cm")||Integer.parseInt(verification_code_id)==2&&verification_code.equals("v33c")||Integer.parseInt(verification_code_id)==3&&verification_code.equals("xvv2")||Integer.parseInt(verification_code_id)==4&&verification_code.equals("zm2m")) {
            if(repeatedPassword.equals(account.getPassword())){
                AccountService.AddAccount(account);
                return "catalog/main";
            }
        }
        else model.addAttribute("failedRegister",false);
        return "account/newAccountForm";
    }

    @PostMapping("/editAccount")
    public String EditAccount(String repeatedPassword,@ModelAttribute Account account,Model model){
        if(repeatedPassword.equals(account.getPassword())){
            AccountService.updateAccount(account);
            return "catalog/main";
        }
        else {
            model.addAttribute("Edit",false);
            return "account/editAccountForm";
        }
    }
}
